package org.dreamfly.netshop.manage;

import java.util.ArrayList;
import java.util.List;

import org.dreamfly.netshop.dao.OrderInfoDao;
import org.dreamfly.netshop.entity.OrderInfo;
import org.dreamfly.netshop.service.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.orm.PropertyFilter.MatchType;
import org.springside.modules.orm.hibernate.EntityManager;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;

/**
 * 用户管理类. 实现领域对象用户的所有业务管理函数. 演示派生DAO层子类的模式,由注入的UserDao封装数据库访问. 通过范型声明继承EntityManager,默认拥有CRUD管理方法. 使用Spring
 * annotation定义事务管理.
 * 
 * @author calvin
 */
// Spring Service Bean的标识.
@Service
// 默认将类中的所有函数纳入事务管理.
@Transactional
public class OrderInfoManager extends EntityManager<OrderInfo, Long> {
    @Autowired
    private OrderInfoDao orderInfoDao;

    /**
     * 实现回调函数,为EntityManager基类的CRUD操作提供DAO.
     */
    @Override
    protected OrderInfoDao getEntityDao() {
        return orderInfoDao;
    }

    /**
     * 重载delte函数,演示异常处理及用户行为日志.
     */
    @Override
    public void delete(Long id) {
        if (id == 1) {
            logger.warn("操作员{}尝试删除超级管理员用户", SpringSecurityUtils.getCurrentUserName());
            throw new ServiceException("不能删除超级管理员用户");
        }

        orderInfoDao.delete(id);
    }

    /**
     * 检查用户名是否唯一.
     * 
     * @return loginName在数据库中唯一或等于orgLoginName时返回true.
     */
    @Transactional(readOnly = true)
    public boolean isLoginNameUnique(String loginName, String orgLoginName) {
        return orderInfoDao.isPropertyUnique("loginName", loginName, orgLoginName);
    }

    public List<OrderInfo> getAllNeedToPay(long MemberId) {
        List<PropertyFilter> list = new ArrayList<PropertyFilter>();
        list.add(new PropertyFilter("member.id", MemberId, MatchType.EQUAL));
        list.add(new PropertyFilter("isPayment", false, MatchType.EQUAL));
        return orderInfoDao.findByFilters(list);
    }

    public List<OrderInfo> getAllSuccess(long MemberId) {
        List<PropertyFilter> list = new ArrayList<PropertyFilter>();
        list.add(new PropertyFilter("member.id", MemberId, MatchType.EQUAL));
        list.add(new PropertyFilter("isConfirm", true, MatchType.EQUAL));
        return orderInfoDao.findByFilters(list);
    }

    public List<OrderInfo> getNeedToShip(long MemberId) {
        List<PropertyFilter> list = new ArrayList<PropertyFilter>();
        list.add(new PropertyFilter("member.id", MemberId, MatchType.EQUAL));
        list.add(new PropertyFilter("isPayment", true, MatchType.EQUAL));
        list.add(new PropertyFilter("isConsignment", false, MatchType.EQUAL));
        return orderInfoDao.findByFilters(list);
    }

    public List<OrderInfo> getNeedToSure(long MemberId) {
        List<PropertyFilter> list = new ArrayList<PropertyFilter>();
        list.add(new PropertyFilter("member.id", MemberId, MatchType.EQUAL));
        list.add(new PropertyFilter("isConfirm", false, MatchType.EQUAL));
        list.add(new PropertyFilter("isConsignment", true, MatchType.EQUAL));
        return orderInfoDao.findByFilters(list);
    }

    public List<OrderInfo> getByConfirm(boolean isConfirm) {
        List<PropertyFilter> list = new ArrayList<PropertyFilter>();
        list.add(new PropertyFilter("isConfirm", isConfirm, MatchType.EQUAL));
        return orderInfoDao.findByFilters(list);
    }

    public List<OrderInfo> getByPayment(boolean isConfirm) {
        List<PropertyFilter> list = new ArrayList<PropertyFilter>();
        list.add(new PropertyFilter("isPayment", isConfirm, MatchType.EQUAL));
        return orderInfoDao.findByFilters(list);
    }

    public List<OrderInfo> getByConsignment(boolean isConfirm) {
        List<PropertyFilter> list = new ArrayList<PropertyFilter>();
        list.add(new PropertyFilter("isConsignment", isConfirm, MatchType.EQUAL));
        return orderInfoDao.findByFilters(list);
    }

    public Page<OrderInfo> getByConfirm(boolean isConfirm, Page<OrderInfo> page) {
        List<PropertyFilter> list = new ArrayList<PropertyFilter>();
        list.add(new PropertyFilter("isConfirm", isConfirm, MatchType.EQUAL));
        return orderInfoDao.findByFilters(page, list);
    }

    public Page<OrderInfo> getByPayment(boolean isConfirm, Page<OrderInfo> page) {
        List<PropertyFilter> list = new ArrayList<PropertyFilter>();
        list.add(new PropertyFilter("isPayment", isConfirm, MatchType.EQUAL));
        return orderInfoDao.findByFilters(page, list);
    }

    public Page<OrderInfo> getByConsignment(boolean isConfirm, Page<OrderInfo> page) {
        List<PropertyFilter> list = new ArrayList<PropertyFilter>();
        list.add(new PropertyFilter("isConsignment", isConfirm, MatchType.EQUAL));
        return orderInfoDao.findByFilters(page, list);
    }
}
